package com.magicalcoder.youyaboot.config;

import com.magicalcoder.lowcode.core.utils.OpenBrowserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 当spring bean 启动完毕 就会执行此类方法
 */
@Component
@Slf4j
public class SpringBeanAllInitFinished implements ApplicationListener<ApplicationReadyEvent> {

    /*默认浏览器安装路径*/
    @Value("${magicalcoder.settings.explorerPath:C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe}")
    private String explorerPath;

    @Value("${server.servlet.context-path:}")
    private String contextPath;
    @Value("${server.port:80}")
    private Integer port;
    @Value("${magicalcoder.lowcode.openExplorer:true}")
    private Boolean openExplorer;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent contextRefreshedEvent) {
        //需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。
        log.info("spring bean 加载完毕");
        if(openExplorer){
            //使用浏览器协助打开管理后台 请勿使用多线程打开 否则出现浏览器不关闭 占用target文件夹情况
            String youyabootUrl = String.format("http://localhost:%s%s/magicalcoder/index.html", 8081, contextPath);
            log.info("正在使用浏览器打开管理后台地址：" + youyabootUrl);
            OpenBrowserUtil.openURL(explorerPath, youyabootUrl);
        }
    }
}
